package com.spring.mvc.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.spring.mvc.bean.CanbLocal;
import com.spring.mvc.service.CanbLocalService;
import com.spring.mvc.util.Constantes;


@Controller
public class CanbLocalController {
	
	@Autowired
	private CanbLocalService canbLocalService;
	
	
	@RequestMapping(value = "/listalocal.do", method = RequestMethod.POST)
	@ResponseBody
	public JTableJSONResponse<Map<String, Object>> listadoLocal(){
		 JTableJSONResponse<Map<String, Object>> jstr=new JTableJSONResponse<Map<String,Object>>();
		try {
			List<Map<String, Object>> listaLocal = canbLocalService.selectAllLocal("");
		   jstr = new JTableJSONResponse<Map<String, Object>>("OK",listaLocal,listaLocal.size());
	
		} catch (Exception e) {
			e.printStackTrace();
		}
			    
	    return jstr;
	}
	
	@RequestMapping(value = "/listadoLocalByFilter.do", method = RequestMethod.POST)
	@ResponseBody
	public JTableJSONResponse<Map<String, Object>> listadoLocalByFilter(String desLocal){
		 JTableJSONResponse<Map<String, Object>> jstr=new JTableJSONResponse<Map<String,Object>>();
		try {
			List<Map<String, Object>> listaLocal = canbLocalService.selectAllLocal(desLocal);
		   jstr = new JTableJSONResponse<Map<String, Object>>("OK",listaLocal,listaLocal.size());
	
		} catch (Exception e) {
			e.printStackTrace();
		}			    
	    return jstr;
	}
	
	@RequestMapping(value = "/registrarlocal.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> registrarLocal(CanbLocal local, HttpSession session) 
	throws Exception {

		Map<String, Object> data = new HashMap<String, Object>();
		try{
			
		local.setDES_USUARIO_REGISTRO(session.getAttribute("desUsuarioLogin").toString());
		if(local.getID_LOCAL()==null){
			canbLocalService.insertLocal(local);
		}
		
		data.put("Record", local);
		data.put("Result", "OK");
		data.put("message", "Se registro satisfacoriamente.");
		}catch (Exception e) {
		e.printStackTrace();	
		//data.put("data", data);
		data.put("Result", "ERROR");
		data.put("message", "En estos momentos estamos en mantenimiento.");
		}
		return data;
	}

	
	@RequestMapping(value = "/actualizarlocal.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> actualizarLocal(CanbLocal local, HttpSession session)
	throws Exception {
		
		Map<String, Object> data = new HashMap<String, Object>();
		try{
		local.setDES_USUARIO_REGISTRO(session.getAttribute("desUsuarioLogin").toString());
		local = canbLocalService.updatebyIdLocal(local);
		data.put("Record", local);
		data.put("Result", "OK");
		data.put("message", "Se actualizo satisfacoriamente.");
		}catch (Exception e) {
		e.printStackTrace();	
		data.put("data", data);
		data.put("success", Boolean.FALSE);
		data.put("message", "En estos momentos estamos en mantenimiento.");
		}
		return data;
	}
	
	
	@RequestMapping(value = "/eliminarlocal.do", method = RequestMethod.POST)
	public @ResponseBody Map<String, ? extends Object> eliminarLocal(Integer idLocal)
	throws Exception {
		
		Map<String, Object> data = new HashMap<String, Object>();
		try{
		CanbLocal	local= new CanbLocal(); 
		local.setID_LOCAL(idLocal);
		local.setST_LOCAL(Constantes.ESTADO_INACTIVO+"");
		local = canbLocalService.updatebyIdLocal(local);
		data.put("Result", "OK");
		data.put("message", "Se actualizo satisfacoriamente.");
		}catch (Exception e) {
		e.printStackTrace();	
		data.put("data", data);
		data.put("Result", "ERROR");
		data.put("message", "En estos momentos estamos en mantenimiento.");
		}
		return data;
	}
	
	

}
